<?php

namespace app\wx\controller;

use think\App;
use think\Controller;


class Index extends Controller
{
    //


    public  function  index(){
        $state=input('accountId');
        $state=1;
        $appid=config('wx.appid');
        $redirect_uri=config('wx.redirect_uri');
        $scope=config('wx.scope');

        $url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$appid.
            '&redirect_uri='.urlencode($redirect_uri).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
//        $res=api($url,$param=[],$method='GET',$header=[]);
        return json([
            'data'=>$url
        ]);
    }
    public  function  pan(){
        $code=input('param.code');

        $openid=$this->pancode($code)['openid'];
        $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.config('wx.appid').'&secret='.config('wx.secret');
        $res=api($url,$param=[],$method='get',$header=[]);

        $res=json_decode($res,TRUE );
        $access_token=$res['access_token'];
        $url2='https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='.$access_token;
        $param2=[
            'touser'=>$openid,

            'template_id'=>'N74t8HfcuaeKvdAIlujkds3eO7RamaCOiY4PCAZsr-8',
            "url"=>"http://company.panview.cn/wx",
            'topcolor'=>"#FF0000",
            "data"=>[
                'title'=>[
                    "value"=>"测试标题",
                    "color"=>"#173177"
                ],
                'datetime'=>[
                    "value"=>"123123",
                    "color"=>"#173177"
                ],

            ]

        ];

        $pan=api($url2,$param2,$method='post',$header=[]);
        dump($pan);

    }
    public  function  pancode($code){
        $url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.config('wx.appid').'&secret='.config('wx.secret').'&code='.$code.'&grant_type=authorization_code';
        $res=api($url,$param=[],$method='GET',$header=[]);
        $res=json_decode($res,TRUE );
        return $res;
    }

}
